package main

import (
	"fmt"
	"log"
	"time"

	rotatelogs "github.com/lestrrat-go/file-rotatelogs"
)

func main() {
	// 配置 rotatelogs
	logf, err := rotatelogs.New(
		// 日志文件名格式
		"./file_rotatelogs_demo/log/myapp.%Y%m%d-%H%M%S.log",
		// 设置最大保存时间(7天)
		//rotatelogs.WithMaxAge(15*time.Second),
		// 设置日志切割时间间隔(1天)
		rotatelogs.WithRotationTime(5*time.Second),
		rotatelogs.WithRotationCount(2),
	)
	if err != nil {
		log.Printf("failed to create rotatelogs: %s", err)
		return
	}

	// 设置日志输出到 rotatelogs
	log.SetOutput(logf)

	// 写入一些测试日志
	for i := 0; i < 60; i++ {
		fmt.Printf("这是测试日志消息 #%d\n", i+1)
		log.Printf("这是测试日志消息 #%d\n", i+1)
		time.Sleep(time.Second)
	}

	fmt.Println("日志已写入，请查看 log 目录")
}
